<template>
  <div class="app-container" ref="container">
    <!-- 固定的搜索和工具栏容器 -->
    <div class="fixed-header">
      <!-- 搜索栏 -->
      <div class="search-bar">
        <el-form :inline="true" :model="queryParams" class="demo-form-inline">
          <el-form-item label="分类名称">
            <el-input
                v-model="queryParams.name"
                placeholder="请输入分类名称"
                clearable
                size="small"
                @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="small" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="small" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
      </div>

      <!-- 工具栏 -->
      <div class="toolbar-right">
        <el-button
            type="primary"
            icon="el-icon-plus"
            size="small"
            @click="handleAdd"
        >新增分类</el-button>
        <el-button size="small" @click="getList">
          <i class="el-icon-refresh"></i> 刷新
        </el-button>
      </div>
    </div>

    <!-- 表格内容区域 -->
    <div class="table-container">
      <!-- 表格容器 -->
      <div class="content-container">
        <el-table
            ref="table"
            v-loading="loading"
            :data="tagList"
            border
            row-key="id"
        >
          <el-table-column prop="id" label="标签ID" width="80" align="center" />
          <el-table-column prop="name" label="标签名称" min-width="200" />
          <el-table-column prop="classid" label="标签分类" width="100" align="center" />
          <el-table-column label="操作" width="200" fixed="right" align="center">
            <template slot-scope="scope">
              <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-edit"
                  @click="handleUpdate(scope.row)"
              >编辑</el-button>
              <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-delete"
                  @click="handleDelete(scope.row)"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>

    <!-- 添加或修改分类对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="标签名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入分类名称" />
        </el-form-item>
        <el-form-item label="分类名称" prop="defaultType">
          <el-input-number v-model="form.classid" :min="1" :max="999" controls-position="right" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'CategoryView',
  data() {
    return {
      // 遮罩层
      loading: false,
      // 分类表格数据
      tagList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        name: undefined
      },
      // 表单参数
      form: {
        id: undefined,
        name: undefined,
        classid: 1
      },
      // 表单校验
      rules: {
        name: [
          { required: true, message: '标签名称不能为空', trigger: 'blur' }
        ],
        classid: [
          { required: true, message: '分类编号不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询分类列表 */
    getList() {
      this.loading = true
      this.axios.post('http://localhost:9999/api-tagclass-service/tagclass/findtag', this.queryParams)
          .then(res => {
            this.tagList = res.data
            this.loading = false
          })
          .catch(() => {
            this.loading = false
            this.$message.error('获取标签列表失败')
          })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        name: undefined
      }
      // 清空选中项
      if (this.$refs.table) {
        this.$refs.table.clearSelection()
      }
      this.getList()
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加标签'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      this.open = true
      this.title = '修改标签'
      this.form = { ...row }
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm('是否确认删除名称为"' + row.name + '"的标签?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.axios.post(`http://localhost:9999/api-tagclass-service/tagclass/delete?id=${row.id}`).then(row => {
          this.$message.success('删除成功')
          this.getList()
        }).catch(row => {
          this.$message.error('删除失败')
        })
        this.getList()
      })
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        name: undefined,
        classid: 1
      }
      if (this.$refs.form) {
        this.$refs.form.resetFields()
      }
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.axios.post('http://localhost:9999/api-tagclass-service/tagclass/add', this.form).then(res => {
                this.$message.success('保存成功')
                this.open = false
                this.getList()
              }
          ).catch(
              () => this.$message.error('保存失败')
          )
        }
      })
    },
    /** 取消按钮 */
    cancel() {
      this.open = false
      this.reset()
    }
  },
  // 分页大小改变
  handleSizeChange(val) {
    this.pageSize = val
    this.getList()
  },
  // 当前页改变
  handleCurrentChange(val) {
    this.pageNo = val
    this.getList()
  },
}
</script>

<style scoped>
.app-container {
  padding: 0;
  background-color: #fff;
  width: 100%;
  overflow: hidden;
  position: relative;
}

.fixed-header {
  position: fixed;
  top: 50px;  /* 与顶部导航栏对齐 */
  left: 210px;  /* 与侧边栏对齐 */
  right: 0;
  background-color: #fff;
  z-index: 999;
  padding: 15px 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  box-shadow: 0 1px 4px rgba(0,21,41,.08);
}

.search-bar {
  flex: 1;
}

.toolbar-right {
  margin-left: 20px;
}

.table-container {
  margin-top: 70px;  /* 为固定头部留出空间 */
  padding: 0 20px;
}

.content-container {
  background-color: #fff;
  border-radius: 4px;
  width: 100%;
  overflow-x: hidden;
}

.el-table {
  margin-bottom: 20px;
}

/* 添加全局样式，隐藏滚动条但保持滚动功能 */
::-webkit-scrollbar {
  display: none;
}

/* 调整表单项的间距 */
.demo-form-inline .el-form-item {
  margin-bottom: 0;
  margin-right: 20px;
}

.demo-form-inline .el-form-item:last-child {
  margin-right: 0;
}
</style>
